package com.qf.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyInterceptor implements HandlerInterceptor {
    //      在 目标handler 执行之前执行
//      客户端   dispatcherServlet
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器");
        //          判断登录状态
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (user != null) {
//            放行  可以执行目标资源
            return true;
        }
//         终端请求 不在执行后续操作
        response.sendRedirect(request.getContextPath() + "/login.jsp");
        return false;
    }
}
